// ==============================================================================================
// SpecialGroup.cs
//
// Created by: NI, 2007.03.28
// Revised by: NI, 2007.11.23
// ==============================================================================================
using System.Collections.Generic;
using System.Linq;
using RibbonExplorer.Entities;
using RibbonExplorer.Storage;

namespace RibbonExplorer.Entities
{
  // ==============================================================================================
  /// <summary>
  /// This class represents a collection of controls belonging to the Office menu.
  /// </summary>
  // ==============================================================================================
  public abstract class SpecialGroup : Group
  {
    #region Private fields

    private Ribbon _ParentRibbon;

    #endregion

    #region Lifecycle methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Creates an instance of this special group.
    /// </summary>
    /// <param name="id">ID of the group.</param>
    /// <param name="ribbon">Parent ribbon of this group.</param>
    // --------------------------------------------------------------------------------------------
    public SpecialGroup(string id, Ribbon ribbon)
      : base(id, null)
    {
      _ParentRibbon = ribbon;
    }

    #endregion

    #region Public properties

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the parent ribbon of this special group.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public override Ribbon ParentRibbon
    {
      get { return _ParentRibbon; }
    }

    #endregion

    #region Overridden methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Obtains the child controls belonging to this group from the specified ribbon table
    /// </summary>
    /// <param name="ribbonTable">Ribbon table instance.</param>
    /// <param name="controlId">ID of the control to get its children</param>
    /// <returns>Controls belonging to the group</returns>
    // --------------------------------------------------------------------------------------------
    public override List<RibbonTableItem> FilterChildren(RibbonTable ribbonTable,
                                                         string controlId)
    {
      var result = from control in ribbonTable.ControlList
                   where control.Group == controlId &&
                         control.Parent == ""
                   orderby control.Ordering
                   select control;

      return new List<RibbonTableItem>(result);
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Obtains the grandchild controls belonging to this group from the specified ribbon table
    /// </summary>
    /// <param name="ribbonTable">Ribbon table instance.</param>
    /// <param name="parentControlId">ID of the control to get its children</param>
    /// <param name="grandParentControlId">ID of the grandparent</param>
    /// <returns>Controls belonging to the group</returns>
    // --------------------------------------------------------------------------------------------
    public override List<RibbonTableItem> FilterGrandChildren(RibbonTable ribbonTable,
                                                              string parentControlId, string grandParentControlId)
    {
      var result = from control in ribbonTable.ControlList
                   where control.Parent == parentControlId &&
                         control.Group == grandParentControlId
                   orderby control.Ordering
                   select control;
 
      return new List<RibbonTableItem>(result);
    }

    #endregion
  }
}